package Meituan.Feature;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Collections;
import java.util.Map;

/**
 * 特征格式化
 * 以sg_mkt开发，
 * @author: leiwenkang
 * @date: 2020/4/24
 * @time: 15:49
 */
@Slf4j
public class FeatureParser {
    public static void main(String[] args) {
        String str = "{\n" +
                "    \"avg_ord_num_90days\":0,\n" +
                "    \"end_order_dt\":\"20180414\",\n" +
                "    \"weekend_min_price\":82,\n" +
                "    \"avg_price\":62.599998474121094,\n" +
                "    \"min_price_usr\":15,\n" +
                "    \"avg_ord_num_30days\":0,\n" +
                "    \"7_cnt\":0,\n" +
                "    \"90_cnt\":1,\n" +
                "    \"avg_ord_num_7days\":0,\n" +
                "    \"ord_num_180days\":0,\n" +
                "    \"ord_num_total_usr\":6,\n" +
                "    \"avg_sub_ord_amt_total\":77.89999961853027,\n" +
                "    \"avg_ord_amt\":76.53333282470703,\n" +
                "    \"alphau2\":0.40214488566578815,\n" +
                "    \"churn_rate\":-1,\n" +
                "    \"max_price_usr\":116.19999694824219,\n" +
                "    \"label_r\":0,\n" +
                "    \"30_cnt\":0,\n" +
                "    \"waimai_app_installed\":{\n" +
                "        \"baiduwaimai\":-1,\n" +
                "        \"dazhongapp\":-1,\n" +
                "        \"jddjapp\":-1,\n" +
                "        \"meituanwaimai\":-1,\n" +
                "        \"meituanapp\":-1,\n" +
                "        \"eleme\":-1\n" +
                "    },\n" +
                "    \"ord_amt_total_usr\":459.1999969482422,\n" +
                "    \"first_order_dt\":\"20150202\",\n" +
                "    \"ord_num_90days\":0,\n" +
                "    \"pay_amt_total\":309.20999694801867,\n" +
                "    \"is_receive_red_7days\":0,\n" +
                "    \"count_nofee_order_num\":0,\n" +
                "    \"avg_pay_amt\":51.53499949133644,\n" +
                "    \"cancel_ord_total_rate\":25,\n" +
                "    \"alphau2_flag\":3,\n" +
                "    \"date_end_to_now\":746,\n" +
                "    \"median\":49,\n" +
                "    \"mt_act_cost_rate\":8.928571487909053,\n" +
                "    \"end_aor_type\":2,\n" +
                "    \"is_cheat\":0,\n" +
                "    \"pay_amt_90days\":0,\n" +
                "    \"first_aor_type\":2,\n" +
                "    \"end_order_source\":41,\n" +
                "    \"date_fst_to_now\":1913\n" +
                "}";
        Map<String, Double> result = parse(str);
        System.out.println(result);

    }

    private static Map<String, Double> parse(String data) {
        if (StringUtils.isBlank(data)) {
            return Collections.emptyMap();
        }

        Map<String, Double> featureMap = Maps.newHashMap();

        try {
            JSONObject json = JSON.parseObject(data);
            JSONObject wmAppInstalledJson = (JSONObject) json.remove("waimai_app_installed");
            featureMap.putAll(JSON.toJavaObject(wmAppInstalledJson, Map.class));
            featureMap.putAll(JSON.toJavaObject(json, Map.class));
        } catch (Exception e) {
            log.error("转化外卖特征平台的特征异常！data : {}", data, e);
        }
        return featureMap;
    }


}
